package LW.Labyrinthe;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.SharedPreferences;
import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class LabyrintheLW extends BaseLiveWallpaperService{
    /** Called when the activity is first created. */


	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	
	private float gravity_ = 9.8f;
	private boolean goRight_;
	private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
	
	private TiledTextureRegion circleFaceTextureRegion_;
	
	private Line centreGauche_, centreDroite_, gauche_, droite_, diagonaleGauche_;
	private Scene scene_;
	private Body cheminGauche_, cheminDroite_, ballBody_;
	private AnimatedSprite ball_;
	private PhysicsWorld physicsWorld_;
	private BitmapTextureAtlas bitmapTextureAtlas_;
	private FixtureDef wallFixtureDef_;
	
	
	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine() {
		Log.d("TAG_Labyrinthe", "onLoadEngine");
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new org.anddev.andengine.engine.Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera));
	}

	@Override
	public void onLoadResources() {
		
		
		this.bitmapTextureAtlas_ = new BitmapTextureAtlas(64, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		Log.d("TAG_Labyrinthe", "onLoadResource1");
		circleFaceTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_, this, "face_circle_tiled1.png", 0, 32, 2, 1); // 64x32
		Log.d("TAG_Labyrinthe", "onLoadResource2");
		this.mEngine.getTextureManager().loadTexture(bitmapTextureAtlas_);
		Log.d("TAG_Labyrinthe", "onLoadResource3");
	}

	@Override
	public void onUnloadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Scene onLoadScene() {
		
		scene_ = new Scene();
		this.scene_.setBackground(new ColorBackground(0, 0, 0));
		
		gauche_  = new Line(50, 0, 50, CAMERA_HEIGHT);
		droite_  = new Line(CAMERA_WIDTH-50, 0, CAMERA_WIDTH-50, CAMERA_HEIGHT);
		centreGauche_ = new Line(200, 0, 200, CAMERA_HEIGHT);
		centreDroite_ = new Line(CAMERA_WIDTH-80, 0, CAMERA_WIDTH-80, CAMERA_HEIGHT);
		diagonaleGauche_ = new Line(50, 400, 100, 500);
		final float ballXGauche = gauche_.getX1() - gauche_.getWidth()/2;
		final float ballYGauche = 30;
		final float ballXDroite = droite_.getX1() - droite_.getWidth()/2;
		final float ballYDroite = CAMERA_HEIGHT;
		
		ball_ = new AnimatedSprite(ballXGauche, ballYGauche, circleFaceTextureRegion_);
		
		goRight_ = true;
		
		physicsWorld_ = new PhysicsWorld(new Vector2(0, 5), false){
			@Override
			public void onUpdate(float pSecondsElapsed) {
				Log.d("TAG_Labyrinthe", ball_.getY()+"");
				boolean update = false;
				if(ball_.getY() > CAMERA_HEIGHT){
						scene_.detachChild(ball_);
						ball_ = new AnimatedSprite(ballXDroite, ballYDroite, circleFaceTextureRegion_);
						physicsWorld_.setGravity(new Vector2(0, -5));
						update = true;
					}
				if(ball_.getY() < 0){
						scene_.detachChild(ball_);
						ball_ = new AnimatedSprite(ballXGauche, ballYGauche, circleFaceTextureRegion_);
						physicsWorld_.setGravity(new Vector2(0, 5));
						update = true;
					}
				if(update){
					ballBody_ = PhysicsFactory.createCircleBody(physicsWorld_, ball_, BodyType.DynamicBody, FIXTURE_DEF);
					physicsWorld_.registerPhysicsConnector(new PhysicsConnector(ball_, ballBody_, true, true));
					scene_.attachChild(ball_);
				}
				super.onUpdate(pSecondsElapsed);
			}
		};
		
		Log.d("TAG_Labyrinthe", "onLoadScene2");
		wallFixtureDef_ = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
		
		cheminGauche_ = PhysicsFactory.createLineBody(physicsWorld_, centreGauche_, wallFixtureDef_);
		cheminGauche_ = PhysicsFactory.createLineBody(physicsWorld_, centreDroite_, wallFixtureDef_);
		cheminDroite_ = PhysicsFactory.createLineBody(physicsWorld_, gauche_, wallFixtureDef_);
		cheminDroite_ = PhysicsFactory.createLineBody(physicsWorld_, droite_, wallFixtureDef_);
		cheminGauche_ = PhysicsFactory.createLineBody(physicsWorld_, diagonaleGauche_, wallFixtureDef_);
		ballBody_ = PhysicsFactory.createCircleBody(physicsWorld_, ball_, BodyType.DynamicBody, FIXTURE_DEF);
		ballBody_.setLinearVelocity(new Vector2(0, 8));
		physicsWorld_.registerPhysicsConnector(new PhysicsConnector(ball_, ballBody_, true, true));
		
		scene_.registerUpdateHandler(this.physicsWorld_);
		scene_.attachChild(ball_);
		scene_.attachChild(centreGauche_);
		scene_.attachChild(centreDroite_);
		scene_.attachChild(gauche_);
		scene_.attachChild(droite_);
		scene_.attachChild(diagonaleGauche_);
		
		Log.d("TAG_Labyrinthe", "onLoadSceneFinal");
		return scene_;
	
	}

	@Override
	protected void onTap(final int pX, final int pY)
	{
		Log.v("TAG_Labyrinthe", "onTap");
	}
	
	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}
}
